import { call, put, fork, select, take } from 'redux-saga/effects'
import actions,{ GET_DATA } from '@/actions/rechargeRecord'
import api from '@/api/rechargeRecord'
import { message } from 'antd'
import util from '@/utils'

function* getData() {
  try {
    const params = yield select(({ rechargeRecordReducer }) => util.pick(rechargeRecordReducer,[
      'endTime',
      'pageNum',
      'pageSize',
      'startTime',
      'orderNum',
      'type'
    ]))
    const { startTime, endTime } = params       
    params.startTime = util.formatDay(startTime)
    params.endTime = util.formatDay(endTime)
    const { isSuccess, data, desc } = yield call(api.getData, params)
    if(isSuccess && data && Array.isArray(data.list)){
      yield put(actions.getDataSuccess(data))
    }else {
      throw new Error(desc)
    }
  } catch (error) {
    yield put(actions.getDataFail())
    message.error(error.message || '获取数据错误')
  }
}

function* watchGetData() {
  while (true) {
    yield take(GET_DATA)
    yield call(getData)
  }
}

export default [
  fork(watchGetData)
]